﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web;
using System.Text;

namespace SharePoint.Silverlight.Clock
{
    public partial class ClockWebPartUserControl : UserControl
    {
        #region Properties
        protected ClockWebPart ParentWebPart
        {
            get { return  this.Parent as ClockWebPart; }
        }

        protected string ClockTitle
        {
            get 
            {

                return (!string.IsNullOrEmpty(ParentWebPart.ClockTitle)) ? ParentWebPart.ClockTitle.Replace(',', '|') : String.Empty;
            }
        }
        protected bool ShowDigitalClock
        {
            get
            {
                return ParentWebPart.ShowDigitalClock;
            }
        }
        protected bool ShowSecondHand
        {
            get
            {
                return ParentWebPart.ShowSecondHand;
            }
        }
        protected bool ShowMilliHand 
        {
            get { return ParentWebPart.ShowMilliHand; }
        }
        protected bool ShowTenthsHand
        {
            get { return ParentWebPart.ShowTenthsHand; }
        }

        protected string UtcOffsetTicks
        {
            get
            {
                string utcOffset = string.Empty;
                try
                {
                    if (!string.Equals("0", ParentWebPart.ClockTimeZoneId))
                    {
                        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(ParentWebPart.ClockTimeZoneId);
                        utcOffset = tzi.BaseUtcOffset.Ticks.ToString();
                    }
                }
                catch { }

                return utcOffset;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
             
        }

        protected string GetInitParams()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("ClockTitle={0}", ClockTitle);
            sb.AppendFormat(",ShowDigitalClock={0}", ShowDigitalClock);
            sb.AppendFormat(",ShowSecondHand={0}", ShowSecondHand);
            sb.AppendFormat(",ShowMilliHand={0}", ShowMilliHand);
            sb.AppendFormat(",ShowTenthsHand={0}", ShowTenthsHand);

            if (!string.IsNullOrEmpty(UtcOffsetTicks))
            {
                sb.AppendFormat(",UtcOffsetTicks={0}", UtcOffsetTicks);
            }

            return sb.ToString();
        }
    }
}
